package com.exam.system.service.impl;

import com.exam.system.dao.ExamMenuDao;
import com.exam.system.entity.ExamMenu;
import com.exam.system.redis.RedisUtils;
import com.exam.system.service.ExamMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName ExamMenuServiceImpl
 * @Description
 * @Author ckl
 * @DATE 2021/5/31 9:14
 * @versions 1.0
 */
@Service
@Transactional
public class ExamMenuServiceImpl implements ExamMenuService {
    @Autowired
    private ExamMenuDao examMenuDao;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 查询菜单，根据角色id
     * @param roleId 角色id
     * @return 返回所有的菜单集合
     */
    @Override
    public Object findMenuByRoleId(Integer roleId) {
        // 判断redis里面有没有这个 examMenuList 再去redis里面查询
        if (!redisUtils.lGet("examMenuList"+roleId,0,-1).isEmpty()) {
            List<Object> objects = redisUtils.lGet("examMenuList" + roleId, 0, -1);
            return objects.get(0);
        }else {
            // 查询所有一级菜单数据
            List<ExamMenu> firstMenus = examMenuDao.findFirstMenus();
            // 根据当前登陆用户id所拥有的角色id查询所拥有的所有菜单id 传进来的是角色id
            List<Integer> menuId = examMenuDao.findMenuIdByRoleId(roleId);
            // 创建集合，用于存放查询到的所有菜单
            List<ExamMenu> examMenuList = new ArrayList<>();
            for (ExamMenu firstMenu : firstMenus) {
                Iterator<ExamMenu> iterator = firstMenu.getExamMenuList().iterator();
                while (iterator.hasNext()) {
                    if (!menuId.contains(iterator.next().getId())) {
                        iterator.remove();
                    }
                }
                examMenuList.add(firstMenu);
            }
            // 将所有一级菜单存入redis
            redisUtils.lSet("examMenuList" + roleId, examMenuList);
            return examMenuList;
        }
    }

    @Override
    public Object findAllMenuId() {
        // 判断redis里面有没有这个 examMenuList 再去redis里面查询
        if (!redisUtils.lGet("examMenuList",0,-1).isEmpty()) {
            List<Object> objects = redisUtils.lGet("examMenuList", 0, -1);
            return objects.get(0);
        }else {
            // 查询所有一级菜单数据
            List<ExamMenu> firstMenus = examMenuDao.findFirstMenus();
            // 根据当前登陆用户id所拥有的角色id查询所拥有的所有菜单id 传进来的是角色id
            List<Integer> menuId = examMenuDao.findAllMenuId();
            // 创建集合，用于存放查询到的所有菜单
            List<ExamMenu> examMenuList = new ArrayList<>();
            for (ExamMenu firstMenu : firstMenus) {
                Iterator<ExamMenu> iterator = firstMenu.getExamMenuList().iterator();
                while (iterator.hasNext()) {
                    if (!menuId.contains(iterator.next().getId())) {
                        iterator.remove();
                    }
                }
                examMenuList.add(firstMenu);
            }
            // 将所有一级菜单存入redis
            redisUtils.lSet("examMenuList", examMenuList);
            return examMenuList;
        }
    }

    @Override
    public ExamMenu insertOneMenu(ExamMenu examMenu) {
        redisUtils.delete("examMenuList");
       examMenu.setIsdel("n");
        examMenu.setStatus("y");
        examMenu.setLevel(1);
        examMenu.setType("null");
        examMenuDao.insertOneMenu(examMenu);
        return null;
    }

    @Override
    public ExamMenu insertTwoMenu(ExamMenu examMenu) {
        examMenu.setIsdel("n");
        examMenu.setStatus("y");
        examMenu.setLevel(2);
        examMenu.setPid(examMenu.getId());
        examMenu.setType("null");
        examMenuDao.insertTwoMenu(examMenu);
        redisUtils.delete("examMenuList");
        return null;
    }

    @Override
    public void updateById(ExamMenu examMenu) {
        redisUtils.delete("examMenuList");
        examMenuDao.updateById(examMenu);
    }

    @Override
    public void deleteById(Integer id) {
        redisUtils.delete("examMenuList");
//        ExamMenu examMenu=new ExamMenu();
//        examMenu.setId(id);
//        if (("null").equals(examMenu.getPid())){
//      List<ExamMenu> list=examMenu.getExamMenuList();
//
//        }



        examMenuDao.deleteById(id);
    }


}
